# Encoding: UTF-8
# 该文件是手动编写的。
# 但在源码结构不改变的情况下通常不需要改写这份文件。

SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_VERSION 1)

cmake_minimum_required(VERSION 3.7)

###
# 工具链设置
###

SET(CMAKE_C_COMPILER_WORKS 1)
SET(CMAKE_C_COMPILER arm-none-eabi-gcc)
SET(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
SET(CMAKE_AR arm-none-eabi-ar)
SET(CMAKE_OBJCOPY arm-none-eabi-objcopy)
SET(CMAKE_OBJDUMP arm-none-eabi-objdump)
SET(SIZE arm-none-eabi-size)
# We don't use C++ here so it's fine to ignore it.

###
# 版型配置
###
SET(BOARD STM32F407VET6)
SET(LINK_SCRIPT "${CMAKE_SOURCE_DIR}/${BOARD}_FLASH.ld")

###
# 编译参数
###
SET(COMMON_FLAGS "-mcpu=cortex-m4 -mtune=cortex-m4 -mfpu=auto -mthumb -mthumb-interwork -ffunction-sections -fdata-sections -fno-common -fmessage-length=0 -fno-asynchronous-unwind-tables -specs=nano.specs")
SET(FPU_FLAGS "-mfloat-abi=hard -mfpu=fpv4-sp-d16")
SET(INC_FLAGS "-Iinc/ -Idriver/inc/")
SET(OPTIMIZATION_FLAGS "-g")
SET(BASIC_FLAGS "${COMMON_FLAGS} ${FPU_FLAGS} ${INC_FLAGS} ${LIB_FLAGS} ${OPTIMIZATION_FLAGS}")
SET(CMAKE_C_FLAGS_INIT "${BASIC_FLAGS} -std=gnu11")
SET(CMAKE_EXE_LINKER_FLAGS "${BASIC_FLAGS} -Wl,-gc-sections,--print-memory-usage -T ${LINK_SCRIPT}")
LINK_DIRECTORIES(lib)

PROJECT(skylab C ASM)
SET(CMAKE_C_STANDARD 11)

add_compile_definitions(STM32F40_41xxx)

###
# 编译指令
###

include_directories(driver/inc)
set_target_properties(${TARGET_NAME} PROPERTIES LINK_DEPENDS ${LINK_SCRIPT})
file(GLOB_RECURSE SOURCE "src/*.c" "src/*/*.c" "src/*.s" "src/*/*.s")
file(GLOB_RECURSE HEADER "inc/*.h" "src/*.h" "src/*/*.h")
add_executable(${PROJECT_NAME}.elf ${SOURCE} ${HEADER} ${LINK_SCRIPT})
target_link_libraries(${PROJECT_NAME}.elf arm_cortexM4lf_math)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
	COMMAND ${CMAKE_OBJCOPY} -Obinary ${PROJECT_NAME}.elf ${PROJECT_NAME}.bin
	COMMENT "Stripping out binary"
)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
	COMMAND doxygen ${PROJECT_SOURCE_DIR}/Doxyfile
	COMMENT "Generating documents"
)
